﻿// ----------------------------------------------------------------------------------
// Microsoft Developer & Platform Evangelism
// 
// Copyright (c) Microsoft Corporation. All rights reserved.
// 
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
// EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES 
// OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
// ----------------------------------------------------------------------------------
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
// ----------------------------------------------------------------------------------

namespace umbraco
{
    using System;
    using System.Globalization;
    using umbraco.ACSExtensions;

    public class MappingRuleTasks : interfaces.ITaskReturnUrl
    {
        private readonly MappingRuleService mappingRuleService;
        private string returnUrl = string.Empty;
        private int userId;

        public MappingRuleTasks()
        {
            this.mappingRuleService = new MappingRuleService();
        }

        public int UserId 
        {
            set
            {
                this.userId = value;
            }
        }

        public int TypeID { get; set; }

        public string Alias { get; set; }

        public int ParentID { get; set; }

        public string ReturnUrl
        {
            get { return this.returnUrl; }
        }

        public bool Save()
        {
            var parameters = this.Alias.Split('|');
            var newMapping = new MappingRule
            {
                Id = Guid.NewGuid(),
                IdentityProvider = new IdentityProviderInfo { Id = parameters[0], Name = parameters[1] },
                MemberGroup = new MemberGroupInfo { Id = int.Parse(parameters[2], CultureInfo.InvariantCulture), Name = parameters[3] },
                ClaimType = parameters[4],
                ClaimValue = parameters[5]
            };

            this.returnUrl = "members/EditMemberGroupMapping.aspx?mappingId=" + newMapping.Id;
            this.mappingRuleService.AddMapping(newMapping);
            return true;
        }

        public bool Delete()
        {
            var mappingId = new Guid(this.Alias);
            this.mappingRuleService.DeleteMapping(mappingId);
            return true;
        }
    }
}